//
//  UIViewController+CViewController.m
//  iosapp
//
//  Created by 吴彬 on 15/12/17.
//  Copyright © 2015年 吴彬. All rights reserved.
//

#import "UIViewController+CViewController.h"
#import <objc/runtime.h>
#import <MBProgressHUD.h>

static const void *HUDKEY = &HUDKEY;

@implementation UIViewController (CViewController)

-(MBProgressHUD *)HUD{
    return objc_getAssociatedObject(self, HUDKEY);
}

-(void)setHUD:(MBProgressHUD *) HUD{
    return objc_setAssociatedObject(self, HUDKEY, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

/**
 *  HUD土司提示
 *
 *  @param message 提示信息
 */
-(void)showHudHint:(NSString *)hint{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = hint;
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:2];
}

/**
 *  HUD风火轮提示
 *
 *  @param view 显示在所属View
 *  @param hint 提示信息
 */
-(void)showHudInView:(UIView *)view hint:(NSString *)hint{
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
    if (hint) {
        HUD.labelText = hint;
    }
    
    [view addSubview:HUD];
    [HUD show:YES];
    [self setHUD:HUD];
    
}

/**
 *  HUD土司信息
 *
 *  @param hint    提示信息
 *  @param yOffset HUD Y轴位置
 */
- (void)showHint:(NSString *)hint yOffset:(float)yOffset {
    //显示提示信息
    UIView *view = [[UIApplication sharedApplication].delegate window];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = NO;
    hud.mode = MBProgressHUDModeText;
    hud.labelText = hint;
    hud.margin = 10.f;
    hud.yOffset = IS_IPHONE_5?200.f:150.f;
    hud.yOffset += yOffset;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:2];
}

/**
 *  隐藏HUD
 */
-(void)hideHud{
    [[self HUD] hide:YES];
}

@end
